// pages/musDetails/musDetails.js
var that;
const app = getApp()
Page({
  /** 页面的初始数据
   *  musList 音乐列表
   *  musStatus 当前播放音乐
   *  playing 当前播放状态   {播放：true,暂停：false}
   *  voiceid 当前播放的音乐ID
   */
  data: {
    musList:[
      {
        voiceid:0,
        url: 'http://fs.w.kugou.com/201804101318/d667030414447b170af53747dff816ea/G129/M05/0A/05/wQ0DAFp9SFyAIZhFADzHW6Qje_8051.mp3',
        logo:'http://haha.boboweiqi.cn/lrtsimg/1.jpg',
        title_name:'此时此刻01',
        list_name:'芒果妈妈睡觉故事01'
      },
      {
        voiceid:1,
        url: 'http://fs.w.kugou.com/201804101330/578694cc88f37ad811052c143af22325/G011/M07/1A/0F/Sw0DAFUMqi2AW5MWADpxoVUIiYY276.mp3',
        logo:'http://haha.boboweiqi.cn/lrtsimg/2.jpg',
        title_name:'此时此刻02',
        list_name:'芒果妈妈睡觉故事01'
      },
      {
        voiceid:2,
        url: 'http://fs.w.kugou.com/201804101351/77accd3e5c514bdd8bd0d6f0ece79a58/G053/M02/06/01/dQ0DAFab3iSAckMbAECAQCXxTeU785.mp3',
        logo:'http://haha.boboweiqi.cn/lrtsimg/3.jpg',
        title_name:'此时此刻03',
        list_name:'芒果妈妈睡觉故事01'
      }
    ],
    musStatus:{},
    playing:true,
    voiceid:0,

    songState: {
      progress: 0,
      currentPosition: '00:00',
      duration: '00:00',
      downloadPercent:0
    }
  },
  // 转换时间格式
  timeToString: function (duration) {
    var str = '';
    var minute = parseInt(duration / 60) < 10 ? ('0' + parseInt(duration / 60)) : (parseInt(duration / 60));
    var second = duration % 60 < 10 ? ('0' + duration % 60) : (duration % 60);
    str = minute + ':' + second;
    return str;
  },
  //播放音乐
  songPlay: function () {
    clearInterval(timer);
    var timer = setInterval(function () {
      // 获取后台音乐播放状态
      wx.getBackgroundAudioPlayerState({
        success: function (res) {
          // 播放状态 1表示播放中
          // console.log(res)
          if (res.status == 1) {
            that.setData({
              playing: true,
              songState: {
                progress: res.currentPosition / res.duration * 100,
                currentPosition: that.timeToString(res.currentPosition),
                duration: that.timeToString(res.duration),
                downloadPercent:res.downloadPercent
              }
            });
          } else {
            that.setData({playing: false});
            clearInterval(timer);
          }
        }
      });
    }, 1000);
  },
  //上一首 <=> 下一首 
  playother(event){
    that.songPlay()
    let upOrDown = event.currentTarget.dataset.other//上首歌，下首歌 按钮切换标识 {-1:上首歌 , 1:下首歌}
    let voiceid = this.data.voiceid//当前音乐id 及列表中的位置
    let mucLength = this.data.musList.length//音乐列表长度
    if(upOrDown == -1){
      voiceid -= 1
      if(voiceid < 0){
        voiceid = this.data.musList.length-1
      }
    }else if(upOrDown == 1){
      voiceid += 1
      if(voiceid == mucLength){
        voiceid = 0
      }
    }
    this.setData({
      'musStatus':this.data.musList[voiceid],
      'voiceid':voiceid
    })
    wx.playBackgroundAudio({
        dataUrl: this.data.musStatus.url,
        title: this.data.musStatus.title_name,
        coverImgUrl: this.data.musStatus.logo
    })//进入页面就开始播放
  },
  //播放 <=> 暂停  
  playingtoggle(event){
    let playstatus = event.currentTarget.dataset.p//得到传值这个播放状态。
    if(playstatus){
      wx.pauseBackgroundAudio()// 暂停
      app.playControl(this)
    }else{
      wx.playBackgroundAudio()// 续播
      app.playControl(this)
    }
  },
  //拖动slider
  museek(event){
    // console.log(event);
    // console.log(event.detail.value);滑动选择器改变的值
    that.setData({
      'songState.progress':event.detail.value
    })
    wx.seekBackgroundAudio({
      position: event.detail.value
    })
    that.songPlay();
  },
  /** 生命周期函数--监听页面加载*/
  onLoad: function (options) {
    that = this
    //当前播放音乐数据插入
    this.setData({
      'musStatus':this.data.musList[2],
      'voiceid':2
    })
    wx.playBackgroundAudio({
        dataUrl: this.data.musStatus.url,
        title: this.data.musStatus.title_name,
        coverImgUrl: this.data.musStatus.logo
    })//进入页面就开始播放
  },
  onReady:function(){
    let that = this

    that.songPlay();



    // wx.seekBackgroundAudio({
    //     position: 100
    // })


  },
  /** 分享*/
  onShareAppMessage: function () {
  
  }
})